﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DHQGHN.DAL.BO.LDVL;
using DHQGHN.DAL.Common;
using DHQGHN.PM0.Models.LDVL;
using DHQGHN.PM0.Controllers;
using DHQGHN.Common;
using DHQGHN.PM0.Common.Attributes;

namespace DHQGHN.PM0.Controllers
{
    public class GiveupTimeController : BaseController
    {
        GiveUpTimeBO giveupTimeBO;

        public ActionResult Index(GiveupTimeModel msModel = null, int pageIndex = 0, string sort = "Id", string sortDir = "ASC", string curentID = "")
        {
            ViewBag.ActiveMenus = ViewBag.ActiveMenus == null ? new System.Collections.Hashtable() : ViewBag.ActiveMenus;
            ViewBag.ActiveMenus[13] = "active";
            ViewBag.ActiveMenus[254] = "active";
            msModel.DELETED = 2;

            var model = Search(msModel, pageIndex, curentID, false);
            return View(model);
        }

        [HttpPost]
        public ActionResult ActionSearch(GiveupTimeModel a = null, int pageIndex = 0, string sort = "Code", string sortDir = "ASC", string curentID = "")
        {
            var model = Search(a, pageIndex, curentID, false);
            return PartialView("~/Views/GiveupTime/_GiveupTimeList.cshtml", model);
        }

        [HttpPost]
        public ActionResult CreatePopupUpdate(string id)
        {
            GiveupTimeModel amodel = null;
            if (!String.IsNullOrEmpty(id))
            {
                giveupTimeBO = new GiveUpTimeBO();
                amodel = new GiveupTimeModel(giveupTimeBO.GetById(id));
            }
            if (amodel == null)
            {
                amodel = new GiveupTimeModel()
                {
                    NAME = "",
                    CODE = "",
                    LS_GIVEUP_TIME_ID = "",
                    DESCRIPTIONS = ""
                };
            }

            if (Request.IsAjaxRequest())
            {
                return PartialView(amodel);
            }
            //RedirectToAction("ManageUsers", "Admin");
            return View(amodel);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ActionSave(GiveupTimeModel model)
        {
            if (Request.IsAjaxRequest())
            {
                giveupTimeBO = new GiveUpTimeBO();
                bool result = giveupTimeBO.Save(model.toDALModel());
                if (result)
                {
                    return Json(JsonResponse.Json200(Resources.Localization.Confirm_Save_Success));
                }
                else
                {
                    return Json(JsonResponse.Json500(Resources.Localization.Confirm_Save_Error));
                }
            }
            return null;
        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult ActionActive(string id)
        {
            if (Request.IsAjaxRequest())
            {
                giveupTimeBO = new GiveUpTimeBO();
                bool result = giveupTimeBO.Active(id);
                if (result)
                {
                    return Json(JsonResponse.Json200(Resources.Localization.Confirm_Save_Success));
                }
                else
                {
                    return Json(JsonResponse.Json500(Resources.Localization.Confirm_Save_Error));
                }
            }
            return null;
        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult ActionDelete(string[] listID)
        {
            if (Request.IsAjaxRequest())
            {
                giveupTimeBO = new GiveUpTimeBO();
                bool result = giveupTimeBO.Delete(listID);
                if (result)
                {
                    return Json(JsonResponse.Json200(Resources.Localization.Confirm_Unuse_Success));
                }
                else
                {
                    return Json(JsonResponse.Json500("Xảy ra lỗi trong quá trình xử lý!"));
                }
            }
            return null;
        }

        private DataPage<GiveupTimeModel> Search(GiveupTimeModel msModel, int pageIndex, string currentId, bool compareEqual)
        {
            giveupTimeBO = new GiveUpTimeBO();
            var DALModel = giveupTimeBO.GetDynamicListPaging(msModel.toDALModel(), PageSize, pageIndex, compareEqual);
            var model = new DataPage<GiveupTimeModel>();
            var lstItem = DALModel.Data;
            foreach (DHQGHN.DAL.Models.LDVL.GiveUpTime giveup in lstItem)
            {
                model.Data.Add(new GiveupTimeModel(giveup));
            }

            GiveupTimeModel giveupTime = null;
            if (!String.IsNullOrEmpty(currentId))
            {
                giveupTime = model.Data.Find(m => m.LS_GIVEUP_TIME_ID == currentId);
                model.CustomData = giveupTime;
            }
            else
            {
                model.CustomData = msModel;
            }
            if (model.Data.Count > 0)
            {
                //Tao component phan trang
                var pagNative = new Pagination
                {
                    ActionName = "ActionSearch",
                    ModelName = "GiveupTime",
                    CurrentPage = pageIndex,
                    InputSearchId = "",
                    TagetReLoadId = "gt-list",

                };
                pagNative.TotalRows = model.Data[0].RECORD_COUNT;
                pagNative.CurrentRow = model.Data.Count;
                ViewBag.Pagination = pagNative;
            }
            return model;
        }

        public JsonResult CheckExistName(string NAME, string LS_GIVEUP_TIME_ID)
        {
            GiveupTimeModel giveuptimeModel = new GiveupTimeModel()
            {
                NAME = NAME.Trim(),
                DELETED = 2
            };

            if (IsExistName(giveuptimeModel, LS_GIVEUP_TIME_ID))
            {
                return Json("Tên này đã tồn tại");
            }
            else
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult CheckExistCode(string CODE, string LS_GIVEUP_TIME_ID)
        {
            GiveupTimeModel giveuptimeModel = new GiveupTimeModel()
            {
                CODE = CODE,
                DELETED = 2
            };
            if (IsExistCode(giveuptimeModel, LS_GIVEUP_TIME_ID))
            {
                return Json("Mã này đã tồn tại");
            }
            else
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
        }

        private bool IsExistCode(GiveupTimeModel model, string id)
        {
            var dataPaging = Search(model, 0, "", true);
            if (dataPaging.Data.Any())
            {
                var lstItem = dataPaging.Data;
                foreach (GiveupTimeModel item in lstItem)
                {
                    if (!(String.IsNullOrWhiteSpace(id) || String.IsNullOrEmpty(id)))
                    {
                        if (!(item.LS_GIVEUP_TIME_ID.ToString().Equals(id)) &&
                            (model.CODE == item.CODE))
                        {
                            return true;
                        }
                    }
                    else
                    {
                        //Kiem tra truong hop them moi
                        return true;
                    }
                }
            }
            return false;
        }

        private bool IsExistName(GiveupTimeModel model, string id)
        {
            var dataPaging = Search(model, 0, "", true);
            if (dataPaging.Data.Any())
            {
                var lstItem = dataPaging.Data;
                foreach (GiveupTimeModel item in lstItem)
                {
                    if (!(String.IsNullOrWhiteSpace(id) || String.IsNullOrEmpty(id)))
                    {
                        if (!(item.LS_GIVEUP_TIME_ID.ToString().Equals(id)) &&
                            (model.NAME == item.NAME))
                        {
                            return true;
                        }
                    }
                    else
                    {
                        //Kiem tra truong hop them moi
                        return true;
                    }
                }
            }
            return false;
        }
	}
}